package com.usian.article.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.usian.article.feign.BehaviorFeigh;
import com.usian.article.mapper.ApArticleMapper;
import com.usian.article.service.ApArticleConfigService;
import com.usian.article.service.ApArticleContentService;
import com.usian.article.service.ApArticleService;
import com.usian.model.article.dto.ArticleHomeDto;
import com.usian.model.article.dto.ArticleDto;
import com.usian.model.article.dto.ArticleInfoDto;
import com.usian.model.article.pojos.ApArticle;
import com.usian.model.article.pojos.ApArticleConfig;
import com.usian.model.article.pojos.ApArticleContent;
import com.usian.model.behavior.pojos.ApBehaviorEntry;
import com.usian.model.behavior.pojos.ApFollowBehavior;
import com.usian.model.behavior.pojos.ApLikesBehavior;
import com.usian.model.common.dtos.ResponseResult;
import com.usian.model.common.enums.AppHttpCodeEnum;
import com.usian.model.user.pojos.ApUser;
import com.usian.utils.threadlocal.AppThreadLocalUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ApArticleServiceImpl extends ServiceImpl<ApArticleMapper, ApArticle> implements ApArticleService {

    @Autowired
    private ApArticleMapper apArticleMapper;

    @Value("${fileServerUrl}")
    private String fileServerUrl;

    @Autowired
    private ApArticleConfigService apArticleConfigService;

    @Autowired
    private ApArticleContentService apArticleContentService;

    @Autowired
    private BehaviorFeigh behaviorFeigh;

    @Override
    public ApArticle saveApArticle(ApArticle apArticle) {
        apArticle.setCreatedTime(new Date());
        this.save(apArticle);
        return apArticle;
    }

    @Override
    public ResponseResult load(Short type, ArticleHomeDto dto) {

        //参数校验
        if (dto == null || dto.getSize() == null ||  dto.getMinBehotTime() == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        if (StringUtils.isBlank(dto.getTag())){
            dto.setTag("__all__");
        }


        List<ApArticle> apArticles = apArticleMapper.loadArticleList(dto, type);

        ResponseResult responseResult = ResponseResult.okResult(apArticles);
        responseResult.setHost(fileServerUrl);

        return responseResult;
    }

    @Override
    public ResponseResult loadArticleInfo(ArticleDto dto) {

        //参数校验
        if (dto == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        HashMap<String, Object> result = new HashMap<>();
        ApArticleConfig articleConfig =
        apArticleConfigService.getOne(Wrappers.<ApArticleConfig>lambdaQuery().eq(ApArticleConfig::getArticleId, dto.getArticleId()));

        if (articleConfig!=null){
            if (!articleConfig.getIsDown() && !articleConfig.getIsDelete()){
                result.put("config",articleConfig);

                ApArticleContent articleContent = apArticleContentService.getOne(Wrappers.<ApArticleContent>lambdaQuery().eq(ApArticleContent::getArticleId, dto.getArticleId()));
                result.put("content",articleContent);
            }
        }


        return ResponseResult.okResult(result);
    }

    @Override
    public ResponseResult loadArticleBehavior(ArticleInfoDto dto) {

        //参数校验
        if (dto == null || dto.getArticleId() == null || dto.getAuthorId() == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        //获取当前登录用户
        ApUser apUser = AppThreadLocalUtils.getUser();
        if (apUser == null){//判断是否登录
            return ResponseResult.errorResult(AppHttpCodeEnum.NEED_LOGIN);
        }

        Map<String, Object> map = new HashMap<>();
        //查询行为实体表
        ApBehaviorEntry apBehaviorEntry = behaviorFeigh.findByUserIdOrEquipmentId(apUser.getId(), dto.getEquipmentId());
        if (apBehaviorEntry != null){

            //查询关注表
            List<ApFollowBehavior> apFollowBehaviors = behaviorFeigh.saveFollowBehavior(apBehaviorEntry.getId(), dto.getArticleId(), apUser.getId());
            if (apFollowBehaviors.size() > 0){
                map.put("isFollow",true);
            }else {
                map.put("isFollow",false);
            }

            //点赞
            ApLikesBehavior like = behaviorFeigh.findLike(apBehaviorEntry.getId(), dto.getArticleId());
            if (like != null){
                map.put("isLike",true);
            }else {
                map.put("isLike",false);
            }
        }

        return ResponseResult.okResult(map);
    }
}
